function DR_set_spontmonitor(varargin)

% Based on the data that is selected, this routine duplicates each selected
% entry, changes the record list to point to the alternate set with
% spontanous activity, tags the entry in the div field with 'spont', and
% sets the durho, s1, s2 and s3 windows for input resistance monitoring and
% spont activity monitoring
% 12=Feb-2008 P. Manis
% 
% based on basic template file for data analysis routines.
% gets the current datac window selection, and does the analysis
% for all selected record groups/blocks
% automatically refreshes the data arrays from the disk file if necessary
% and also shows how to generate a figure.
% Plugin_template2 shows how to  access the main database rather than
% specific data files/data (in other words, how to summarize analyzed data)
% 8/06 P. Manis

% the main routine is just a wrapper to handle the multiple selection and
% catch execution errors.
%
% Note: when testing, set the testflag to 1 so that we DO NOT do try/catch.
% you need to do this to catch the errors in the _go routine.
%


if(nargin == 0)
    noq = 0;
else
    noq = varargin{1};
end;

sf = getmainselection; % read the list of selected traces
testflag = 0;

if(sf > 0)
    pflag = getplotflag; % read teh plot flag (used in the routine template2)
    
    QueMessage('DR_set_spontmonitor - table manipulation', 1); % clear the que
    for i = 1:length(sf)
        if(testflag == 0)
   %         try
                template_go(sf(i), pflag, noq);
     %       catch
     %           fprintf(1, 'Plugin DR_set_spontmonitor Failed execution for sf = %d\n  ***[File: %s, records: %d - %d ]\n', ...
     %               sf(i), CONTROL(sf(i)).filename, CONTROL(sf(i)).recbeg, CONTROL(sf(i)).recend);
     %       end;
        else
            template_go(sf(i), 0, noq); % also turn off plot flag when testing
        end;

    end;
end;
%datac('sortcontrol'); % do a sort at the end to put it all in order
%resequence; % make sure all seqnames are correct.

%--------------The real analysis routine------------------

function template_go(sf, pflag, noq) %#ok<INUSD>

% access to all of the data and parameters
global CONTROL

newseq = make_sequence(CONTROL(sf).filename, CONTROL(sf).recbeg+1); % test whehter we already have a sequence like this
n1 = strmatch(newseq, {CONTROL.seqname}); % find if it exists already
if(isempty(n1))
    n = length(CONTROL);
    CONTROL(n+1) = CONTROL(sf); % make duplicate here
    n1 = n + 1;
    CONTROL(n1).recbeg = CONTROL(n1).recbeg + 1;
    CONTROL(n1).reclist = sprintf('%d;%d/2', CONTROL(n1).recbeg, CONTROL(n1).recend);
    CONTROL(n1).seqname = make_sequence(CONTROL(n1).filename, CONTROL(n1).recbeg); % fix the sequence
else
fprintf(1, '%s exists in database, just updating parameters\n', CONTROL(sf).filename);
end;
CONTROL(n1).div = 'sp';
CONTROL(n1).durho = 5;
CONTROL(n1).durs1 = 50;
CONTROL(n1).durs2 = 240;
CONTROL(n1).durs3 = 600;

QueMessage (sprintf('%s duplicated and tagged', CONTROL(n1).filename), 1);


